package com.qc31.monitorlib.ui.popup

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import com.qc31.baselib.databinding.IncludeSelectSureBinding
import com.qc31.baselib.rx.textChangesNoLong
import com.qc31.baselib.utils.CacheMMKV
import com.qc31.baselib.utils.toast
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.IncludeRecyclerWrapBinding
import com.qc31.monitorlib.databinding.PopupMapTimeBinding
import com.qc31.monitorlib.ui.adapter.RefreshTimeAdapter
import com.qc31.monitorlib.uitls.DataUtil
import io.reactivex.rxjava3.disposables.Disposable

/** @Author Jasper @Time 2020/10/29 @Desc 刷新时间 */
class MapTimePopup(
    private val context: Context, private val callBack: OnTimeCallBack
) : PopupWindow() {
    private val binding = PopupMapTimeBinding.inflate(LayoutInflater.from(context))
    private val sureVB = IncludeSelectSureBinding.bind(binding.root)
    private val recyclerVB = IncludeRecyclerWrapBinding.bind(binding.root)
    var time = 0
    private val mAdapter by lazy { RefreshTimeAdapter() }

    init {
        contentView = binding.root

        initView()
        initRecycler()
        isFocusable = true //popupwindow设置焦点
        isOutsideTouchable = false //点击外面窗口消失
        setBackgroundDrawable(ColorDrawable(-0x56000000)) //设置背景
        width = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的高
    }

    private fun initRecycler() {
        recyclerVB.recyclerWrap.layoutManager = LinearLayoutManager(context)
        recyclerVB.recyclerWrap.adapter = mAdapter.apply {
            setOnItemClickListener { adapter, view, position ->
                index = position
                notifyDataSetChanged()
                binding.editRefreshTime.isEnabled = false
                binding.ivRefreshTime.isSelected = false
            }
            setNewInstance(DataUtil.refreshTimes)
        }

    }

    var subscribe: Disposable? = null
    private var refreshTime: String = ""

    private fun initView() {
        refreshTime = CacheMMKV.decodeString("refresh_marker_time")
        binding.editRefreshTime.setText(refreshTime)

        subscribe = binding.editRefreshTime.textChangesNoLong().subscribe {
            if (it.isNotEmpty()) refreshTime = it
        }
        sureVB.tvSure.setText(R.string.desc_com_sure)

        sureVB.tvSure.setOnClickListener {
            CacheMMKV.encode("refresh_marker_index", mAdapter.index)
            if (mAdapter.index == -1) {
                val customTime = binding.editRefreshTime.text.toString()
                if (customTime.isNotEmpty() && customTime.toInt() > 0 && customTime.toInt() < 100) {
                    time = customTime.toInt() * 60
                    callBack.onBack(time)
                    dismiss()
                    CacheMMKV.encode("refresh_marker_time", customTime)
                } else {
                    R.string.hint_toast_refresh_time.toast(context)
                }
            } else {
                time = mAdapter.data[mAdapter.index].value.toInt()
                callBack.onBack(time)
                dismiss()
            }

        }

        binding.llTimeRefresh.setOnClickListener {
            mAdapter.index = -1
            mAdapter.notifyDataSetChanged()
            binding.ivRefreshTime.isSelected = true
            binding.editRefreshTime.isEnabled = true
        }

        binding.ivClose.setOnClickListener {
            val customTime = CacheMMKV.decodeString("refresh_marker_time")
            binding.editRefreshTime.setText(customTime)
            dismiss()
        }
    }

    fun showPopup(view: View) {
        mAdapter.index = CacheMMKV.decodeInt("refresh_marker_index")
        if (mAdapter.index == -1) {
            binding.ivRefreshTime.isSelected = true
            binding.editRefreshTime.isEnabled = true
            val customTime = CacheMMKV.decodeString("refresh_marker_time")
            binding.editRefreshTime.setText(customTime)
        } else {
            binding.editRefreshTime.isEnabled = false
            binding.ivRefreshTime.isSelected = false
        }
        mAdapter.notifyDataSetChanged()
        showAtLocation(view, Gravity.CENTER, 0, 0)
    }

    fun destroy() {
        subscribe?.dispose()
    }


    interface OnTimeCallBack {
        fun onBack(time: Int)
    }

}